package com.ma.chapter4.t1;

import lombok.extern.slf4j.Slf4j;

import static com.ma.util.Sleeper.sleep;

/**
 * @ClassName Test4
 * @Author: mayongqiang
 * @DATE 2022/3/4 19:47
 * @Description:
 */
@Slf4j(topic = "m.test")
public class Test4 {
    public static void main(String[] args) throws InterruptedException {
        test1();
    }

    private static void test1() throws InterruptedException {
        /**
         * interrupt()方法：打断sleep的线程
         */
        Thread t1 = new Thread(() -> {
            sleep(1);
        }, "t1");
        t1.start();
        sleep(0.5);
        t1.interrupt();
        log.debug(" 打断状态: {}", t1.isInterrupted());
    }

    private static void test2() throws InterruptedException {
        Thread t2 = new Thread(() -> {
            while (true) {
                Thread current = Thread.currentThread();
                boolean interrupted = current.isInterrupted();
                if (interrupted) {
                    log.debug(" 打断状态: {}", interrupted);
                    break;
                }
            }
        }, "t2");
        t2.start();
        sleep(0.5);
        t2.interrupt();
    }
}
